#include "samplelistener.h"
#include "../mapobjects/sampleobjects.h"
#include "../mapobjects/geometry.h"
#include "../mapobjects/objectstorage.h"

#include <QtCore/QObject>
#include <QtCore/QPoint>

SampleListener::SampleListener( Viewer * viewer )
: viewer ( viewer )
{
	connect ( viewer, SIGNAL ( pointClicked ( const QPointF & )), 
		this, SLOT ( onClickEvent ( const QPointF & )));
	connect ( viewer, SIGNAL ( mapLoaded ()), this, SLOT ( onMapLoaded()));
}

void SampleListener::onClickEvent ( const QPointF & pt )
{
	QPointF p2D = QPointF ( pt.x(), pt.y() );
	points.push_back ( p2D );
	if ( points.size() > 1 )
	{
		mapObject = new Route ();
		Polyline * polyline = new Polyline ( points, mapObject );
		mapObject -> addPrimitive ( polyline );
		viewer -> addCosmetics ( mapObject );
		viewer -> update ();
		points.clear();
	}
}

void SampleListener::onMapLoaded ()
{
	ObjectStorage * storage = viewer -> getStorage();

	vector<MapObject *> mapObjects;
	storage -> getMapObjectListOfType ( std::string ( "Road" ), mapObjects );
	for ( size_t i = 0; i < mapObjects.size(); ++i )
	{
		vector<PrimitiveBase *> polylines;
		mapObjects[i] -> getPrimitiveListOfType ( e_PrimTypePolyline, polylines );
		for ( size_t j = 0; j < polylines.size(); ++j )
		{
			Polyline * polyline = reinterpret_cast<Polyline *> ( polylines[j] );
			vector<QPointF> points;
			polyline -> getPoints ( points );
		}
	}
}